package me.jessyan.armscomponent.app.mvp.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.BezierPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.di.component.DaggerExploreHomeComponent;
import me.jessyan.armscomponent.app.mvp.contract.explore.ExploreHomeContract;
import me.jessyan.armscomponent.app.mvp.presenter.explore.ExploreHomePresenter;
import me.jessyan.armscomponent.app.mvp.ui.adapter.ExplorePagerAdapter;
import me.jessyan.armscomponent.app.mvp.ui.widget.ScaleTransitionPagerTitleView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * <p>
 * Author: Taozebi
 * Date: 08/08/2021 15:19
 * Description: describe of this class
 */

@Route(path = RouterHub.EXPLORE_HOMEFRAGMENT)
public class ExploreHomeFragment extends BaseFragment<ExploreHomePresenter> implements ExploreHomeContract.View {
    private static final String[] CHANNELS = new String[]{"视频课", "音频课", "电子书", "锦囊"};
    private List<String> mDataList = Arrays.asList(CHANNELS);
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private FragmentContainerHelper mFragmentContainerHelper = new FragmentContainerHelper();
//    @BindView(R.id.explore_view_pager)
//    ViewPager mViewPager;
    @BindView(R.id.explore_magic_indicator)
    MagicIndicator magicIndicator;
    ExplorePagerAdapter mPagerAdapter;

    public static ExploreHomeFragment newInstance() {
        ExploreHomeFragment fragment = new ExploreHomeFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerExploreHomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.explore_fragment_home, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initFragments();
//        initViewPager();

        initMagicIndicator();
        mFragmentContainerHelper.handlePageSelected(0, false);
        switchPages(0);
    }

    private void switchPages(int index) {
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment;
        for (int i = 0, j = mFragments.size(); i < j; i++) {
            if (i == index) {
                continue;
            }
            fragment = mFragments.get(i);
            if (fragment.isAdded()) {
                fragmentTransaction.hide(fragment);
            }
        }
        fragment = mFragments.get(index);
        if (fragment.isAdded()) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.add(R.id.explore_fl_container, fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();
    }

    private void initFragments(){
        mFragments.add(getExploreVideoFragment());
        mFragments.add(getExploreAudioFragment());
        mFragments.add(getExploreEbookFragment());
        mFragments.add(getExploreSkillBagFragment());
    }

    private void initViewPager(){
//        mPagerAdapter = new ExplorePagerAdapter(mDataList);
//        mViewPager.setAdapter(mPagerAdapter);
    }
    private void initMagicIndicator() {
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.public_titleNormalColor));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.public_titleSelectedColor));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        mViewPager.setCurrentItem(index);
                        mFragmentContainerHelper.handlePageSelected(index);
                        switchPages(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                BezierPagerIndicator indicator = new BezierPagerIndicator(context);
//                indicator.setColors(Color.parseColor("#ff4a42"), Color.parseColor("#fcde64"), Color.parseColor("#73e8f4"), Color.parseColor("#76b0ff"), Color.parseColor("#c683fe"));
                indicator.setColors(getResources().getColor(R.color.public_titleSelectedColor),getResources().getColor(R.color.public_titleSelectedColor),getResources().getColor(R.color.public_titleSelectedColor),getResources().getColor(R.color.public_titleSelectedColor));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        mFragmentContainerHelper.attachMagicIndicator(magicIndicator);

//        ViewPagerHelper.bind(magicIndicator, mViewPager);
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }


    public static Fragment getExploreVideoFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouterHub.EXPLORE_HOME_VIDEO_FRAGMENT).navigation();
        return fragment;
    }
    public static Fragment getExploreAudioFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouterHub.EXPLORE_HOME_AUDIO_FRAGMENT).navigation();
        return fragment;
    }

    public static Fragment getExploreEbookFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouterHub.EXPLORE_HOME_EBOOK_FRAGMENT).navigation();
        return fragment;
    }

    public static Fragment getExploreSkillBagFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouterHub.EXPLORE_HOME_SKILLBAG_FRAGMENT).navigation();
        return fragment;
    }
}
